/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.merak.core.configuration.plugin;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * This class represents a Service in terms of SPI (Service Provider Interfaces).
 * As best practice, Service.name corresponds to the Java Interface class
 * used to declare the SPI specification or, in other words, application dependencies.
 * An unique service can have one or more providers, it is, the plugins theirselves.
 *
 * @author fmarinho
 */

public class Service {

	//~ Attributes ///////////////////////////////////////////////////////////////
	//****************************************************************************
	private String         name      = null;
	private List<Provider> providers = new LinkedList<Provider>();

	//~ Constructors /////////////////////////////////////////////////////////////
	//****************************************************************************
	//~ Methods //////////////////////////////////////////////////////////////////
	//****************************************************************************
	public String getName() {
		return this.name;
	}
	//****************************************************************************
	public void setName(String name) {
		this.name = name;
	}
	//****************************************************************************
	public Provider getProvider() {
		return this.providers.isEmpty() ? null : this.providers.get(0);
	}
	//****************************************************************************
	public void setProviderClass(String provider) {

		if ( provider!=null && !this.hasProvider(provider) ) {
			this.providers.add(new Provider(provider));
		}

	}
	//****************************************************************************
	public void addProvider(Provider provider) {

		if (provider!=null && !this.hasProvider(provider.getClazz()) ) {
			this.providers.add(provider);
		}

	}
	//****************************************************************************
	public boolean hasProvider(String provider) {

		for (Provider p : this.providers) {
			if ( p.getClazz().equals(provider) ) return true;
		}
		return false;

	}
	//****************************************************************************
	public boolean hasProviders() {

		return !this.providers.isEmpty();

	}
	//****************************************************************************
	public Object getInstance()
		throws ClassNotFoundException, InstantiationException, IllegalAccessException
	{

		if (this.providers.isEmpty()) {
			throw new InstantiationException (
				"No providers have been specified for service '" + this.name +
				"'. You must specify at least one provider to use the service."
			);
		}
		return this.providers.get(0).getInstance() ;

	}
	//****************************************************************************
	public List<Provider> getProviders() {
		return Collections.unmodifiableList(this.providers);
	}
	//****************************************************************************
	@Override
	public String toString() {
		return this.name+"("+this.providers.size()+") = "+this.getProvider();
	}
	//****************************************************************************

}
